//
//  UIScrollView+Header.m
//  BESTKEEP
//
//  Created by 武探 on 2016/10/17.
//  Copyright © 2016年 YISHANG. All rights reserved.
//

#import "UIScrollView+BKGDHeader.h"
#import <objc/runtime.h>

@implementation UIScrollView (BKGDHeader)

static const char BKGD_headerViewKey = '\0';

-(void)setBKGD_headerView:(UIView *)BKGD_headerView {
    objc_setAssociatedObject(self, &BKGD_headerViewKey, BKGD_headerView, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    
    if(BKGD_headerView) {
        CGRect frame = BKGD_headerView.frame;
        frame.origin.y = -CGRectGetHeight(frame);
        BKGD_headerView.frame = frame;
        
        UIEdgeInsets contentInset = self.contentInset;
        contentInset.top += CGRectGetHeight(BKGD_headerView.frame);
        self.contentInset = contentInset;
        
        [self addSubview:BKGD_headerView];
        [self sendSubviewToBack:BKGD_headerView];
        
        
    } else {
        if(self.BKGD_headerView) {
            [self.BKGD_headerView removeFromSuperview];
        }
    }
}

-(UIView *)BKGD_headerView {
    return objc_getAssociatedObject(self, &BKGD_headerViewKey);
}

@end
